//
//  CZMainViewController.swift
//  WeiBO28
//
//  Created by 飞奔的羊 on 16/5/9.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SVProgressHUD

class CZMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
//添加只控制器的两种方式
        //viewControllers
        let czTabBar = CZTabBar()
        czTabBar.czDelegate = self
        
        czTabBar.composeBUttonClouse = { [weak self] in
            print("~~~");
			if CZUserAccountViewModel.shareUserAccount.isLogin {
			
				self?.shouwComposeVc()
			} else {
		SVProgressHUD.showErrorWithStatus("未登录,请先登录")
			
			}
			
        }
        
        
        //使用 KVC 设置只读属性,设置自定义 tabbar
        setValue(czTabBar, forKey: "tabBar")
        
        
        //self.tabBar = czTabBar
        
//     addChildViewController()
         addChildViewController(CZHomeTableViewController(), title: "首页", imageName: "tabbar_home")
         addChildViewController(CZMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
         addChildViewController(CZDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
         addChildViewController(CZProfileTableViewController(), title: "我的", imageName: "tabbar_profile")
        
      
        
        
    }

    func addChildViewController(childController: UIViewController, title: String, imageName: String) {
        
        //设置全局统一的 tabbar 选择中颜色,这句代码执行的越早越好.放到 APPDelegate 里面
        UITabBar.appearance().tintColor = UIColor.orangeColor()
        
//        childController.tabBarItem.title = title
//        childController.navigationItem.title = title
        
        //统一文字
        childController.title = title
        //设置tab 文字
        childController.tabBarItem.image = UIImage(named: imageName)
          childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")
        
        //修改渲染模式
//        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        //
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
        
//        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(14)], forState: .Normal)
          let nav = UINavigationController(rootViewController:childController)
        //添加子视图控制器
        addChildViewController(nav)
        
    }

	//进入发微博界面
	private func shouwComposeVc() {
	let composeVc = CZComPoseViewController()
		let composeNav = UINavigationController(rootViewController: composeVc)
		
		presentViewController(composeNav, animated: true, completion: nil)
	
	}


}

extension CZMainViewController: CZTBDelegate {

    func didSelectedComposeBUtton() {
        print("代理来的")
    }
}
